package com.my.service.impl;

import com.my.service.BusinessService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Resource
    private LoadBalancerClient loadBalancerClient;//ribbon负载均衡器

    private String getUrl(String str) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("business-provider");
        return "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/business/" + str;
    }

    @Override
    public String loginByPassword(String username, String password) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("username", username);
        map.add("password", password);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("loginByPassword"), request, String.class);
        return response.getBody();
    }

    @Override
    public String loginByFace(String MD5) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("MD5", MD5);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("loginByFace"), request, String.class);
        return response.getBody();
    }

    @Override
    public String loginByPhone(String phone, String code) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("phone", phone);
        map.add("code", code);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("loginByPhone"), request, String.class);
        return response.getBody();
    }

    @Override
    public String loginByEmail(String email, String code) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("email", email);
        map.add("code", code);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("loginByEmail"), request, String.class);
        return response.getBody();
    }

    @Override
    public String sendPhoneMessage(String phone) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("phone", phone);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("sendPhoneMessage"), request, String.class);
        return response.getBody();
    }

    @Override
    public String sendEmailMessage(String email) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("email", email);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("sendEmailMessage"), request, String.class);
        return response.getBody();
    }

    @Override
    public String register(String username, String password, String shopName, String face, String phone, String email) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("username", username);
        map.add("password", password);
        map.add("shopName", shopName);
        map.add("face", face);
        map.add("phone", phone);
        map.add("email", email);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("register"), request, String.class);
        return response.getBody();
    }

    @Override
    public String getAuthor(HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("author"), request, String.class);
        return response.getBody();
    }
}
